﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucAddVendor : System.Web.UI.UserControl
{
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        if (!IsPostBack)
        {
            if (Session["Product_Add/EditVendor"].ToString() == "Edit")
            {
                LoadData();
                SearchProduct();
            }
            SearchProduct();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Back();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Product_Add/EditVendor"].ToString() == "Add")
        {
            Save();
        }
        else
        {
            Edit();
        }
    }

    protected void gvwProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwProducts.PageIndex = e.NewPageIndex;
        SearchProduct();
    }

    void SearchProduct()
    {
        try
        {
            DataSet dsProducts;
            string criteria = string.Empty;
           
            _products.SearchProduct(criteria,Session["Product_SearchProduct"].ToString(), out dsProducts);
            gvwProducts.DataSource = dsProducts;
            gvwProducts.Columns[1].Visible = true;
            gvwProducts.DataBind();
            gvwProducts.Columns[1].Visible = false;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Product Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    void LoadData()
    {
        try
        {
            string condition = "VendorID = '" + ((string[])Session["Product_VendorIDs"])[0].ToString() + "'";
            DataSet dsVendorEdit = _products.GetData("VendorList", condition);
           
            txtName.Text = dsVendorEdit.Tables[0].Rows[0]["VendorName"].ToString();
            txtAddress.Text = dsVendorEdit.Tables[0].Rows[0]["Address"].ToString();
            txtPlace.Text = dsVendorEdit.Tables[0].Rows[0]["Place"].ToString();
            txtState.Text = dsVendorEdit.Tables[0].Rows[0]["State"].ToString();
            txtPin.Text = dsVendorEdit.Tables[0].Rows[0]["Pin"].ToString();
            txtPhone.Text = dsVendorEdit.Tables[0].Rows[0]["Phone"].ToString();
            txtEmail.Text = dsVendorEdit.Tables[0].Rows[0]["Email"].ToString();
            txtPrimaryPerson.Text = dsVendorEdit.Tables[0].Rows[0]["PrimaryContactPerson"].ToString();
            txtPrimaryNumber.Text = dsVendorEdit.Tables[0].Rows[0]["PrimaryContactMobile"].ToString();
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Load Data Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Save()
    {
        try
        {
            Vendor vendor = new Vendor();

            vendor.VendorName = txtName.Text;
            vendor.Address = txtAddress.Text;
            vendor.Place = txtPlace.Text;
            vendor.State = txtState.Text;
            vendor.Pin = txtPin.Text;
            //if ((txtLandCode.Text != string.Empty) & (txtLand.Text != string.Empty))
            //    vendor.VendorLandNumber = txtLandCode.Text + " " + txtLand.Text;
            //else if (txtLand.Text != string.Empty)
            //    vendor.VendorLandNumber = txtLand.Text;
            vendor.Phone = txtPhone.Text;
            vendor.Email = txtEmail.Text;
            vendor.PrimaryContactPerson = txtPrimaryPerson.Text;
            vendor.PrimaryContactMobile = txtPrimaryNumber.Text;

            if (Session["Product_Add/EditVendor"].ToString() == "Add")  // Add New Vendor
            {
                if (_products.AddVendor(vendor))
                {
                    Response.Write("<Script>alert('Added Successfully !')</Script>");
                    Clear();
                }
                else
                    Response.Write("<Script>alert('Already Exist !')</Script>");
            }

            else if (Session["Product_Add/EditVendor"].ToString() == "Edit")  // Edit Vendor
            {
                vendor.VendorID = ((string[])Session["Product_VendorIDs"])[0].ToString();
                _products.EditVendor(vendor);
                Back();
            }


        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Vendor Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Edit()
    {
        try
        {
            Vendor vendor = new Vendor();
            vendor.VendorID = ((string[])Session["Product_VendorIDs"])[0].ToString();
            vendor.VendorName = txtName.Text;
            vendor.Address = txtAddress.Text;
            vendor.Place = txtPlace.Text;
            vendor.State = txtState.Text;
            vendor.Pin = txtPin.Text;
            vendor.Phone = txtPhone.Text;
            vendor.Email = txtEmail.Text;
            vendor.PrimaryContactPerson = txtPrimaryPerson.Text;
            vendor.PrimaryContactMobile = txtPrimaryNumber.Text;

            _products.EditVendor(vendor);
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Edit Vendor Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Clear()
    {
        try
        {
            txtName.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPlace.Text = string.Empty;
            txtState.Text = string.Empty;
            txtPin.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtPrimaryPerson.Text = string.Empty;
            txtPrimaryNumber.Text = string.Empty;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Clear Function Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Back()
    {
        try
        {

            Session["UserManager_Control"] = "Production/ucVendorList.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Navigate Back Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }     
        
    }

}
